﻿/*
Digraph (library for building, traversal, comparison and saving of directed
graph).

Copyright (C) 2014 Dmitry Merzagitovich Arslanov

This library is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the Free
Software Foundation; either version 2.1 of the License, or (at your option) any
later version.

This library is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
*/

// How to enumerate vertices in digraph.

namespace CustomTypes
{
    public class Node
    {
        public string Name;

        public double Weight;

        public Node(string name)
        {
            Name = name;
        }
    }

    public class Edge
    {
        public double Length;
    }
}

namespace Sample6
{
    using CustomTypes;

    using Vertex = Digraph.Vertex<CustomTypes.Node, CustomTypes.Edge>;
    using Builder = Digraph.Builder<CustomTypes.Node, CustomTypes.Edge>;

    public static class App
    {
        public static void Main()
        {
            Builder builder = new Builder();
            Vertex v1 = builder.NewVertex(new Node("v1"));
            Vertex v2 = builder.NewVertex(new Node("v2"));
            Vertex v3 = builder.NewVertex(new Node("v3"));
            Vertex v4 = builder.NewVertex(new Node("v4"));
            Vertex v5 = builder.NewVertex(new Node("v5"));
            Vertex v6 = builder.NewVertex(new Node("v6"));
            Vertex v7 = builder.NewVertex(new Node("v7"));
            builder.NewArc(v1, v2);
            builder.NewArc(v1, v3);
            builder.NewArc(v2, v4);
            builder.NewArc(v2, v5);
            builder.NewArc(v3, v6);
            builder.NewArc(v3, v7);
            // Enumeration begins from sources and at first goes through
            // vertices of one level.
            bool startFromSink = false;
            bool enumerateInWidth = false;
            builder.Digraph.ForEachVertex(startFromSink, enumerateInWidth,
                // Lambda to process each vertex.
                v =>
                {
                    if (v.IncomingArcs.Count == 0)
                    {
                        v.Content.Weight = 1.0;
                    }
                    else
                    {
                        v.Content.Weight =
                            v.IncomingArcs[0].FromVertex.Content.Weight + 1.0;
                    }
                });
        }
    }
}
